<template>
  <el-card class="box-card" >
    <div slot="header" class="clearfix">
      <breadcrumb>编辑文章</breadcrumb>
    </div>
    <el-form ref="refForm" :rules="rules" :model="form" label-width="80px">
      <el-form-item label="标题" prop="title">
        <el-input v-model="form.title"></el-input>
      </el-form-item>
      <el-form-item label="内容：" class="publish-container" prop="content">
        <quill-editor v-model="form.content" :options="editorOption"></quill-editor>
      </el-form-item>
      <el-form-item label="封面">
        <el-radio-group v-model="form.coverType" @change="hImageTypeChange">
          <el-radio :label="1">单图</el-radio>
          <el-radio :label="3">三图</el-radio>
          <el-radio :label="0">无图</el-radio>
          <el-radio :label="-1">自动</el-radio>
        </el-radio-group>
        <!-- 实现封面组件的引用
          只有是1和3的时候才显示出来-->
        <el-row :gutter="50" class="img_list">
          <el-col v-for="(item, idx) in form.coverType"
            :key="idx" :xs="12" :sm="6" :md="6" :lg="4">
            <cover v-model="form.coverImages[idx]"></cover>
          </el-col>
        </el-row>
      </el-form-item>
      <channel v-model="form.channelId"></channel>
      <el-form-item>
        <el-button type="primary" @click.native="hEdit" :loading="form.isloading">确认编辑</el-button>
      </el-form-item>
    </el-form>
  </el-card>
</template>

<script>
// 1.完成布局和数据回填写
// 2.解决channel的数据回填

// 引入富文本编辑器
import 'quill/dist/quill.core.css'
import 'quill/dist/quill.snow.css'
import 'quill/dist/quill.bubble.css'
// 导入组件
import { quillEditor } from 'vue-quill-editor'
export default {
  components: {
    quillEditor
  },
  name: 'eidt',
  data () {
    return {
      // 富文本配置对象
      editorOption: {
      // 占位配置
        placeholder: '',
        modules: {
          // 配置工具栏
          toolbar: [
            ['bold', 'italic', 'underline', 'strike'],
            ['blockquote', 'code-block'],
            [{ header: 1 }, { header: 2 }],
            [{ list: 'ordered' }, { list: 'bullet' }],
            [{ indent: '-1' }, { indent: '+1' }],
            ['image']
          ]
        }
      },
      draft: false,
      form: {
        title: '',
        content: '',
        cover: '',
        coverType: 0,
        channelId: 0,
        coverImages: [],
        isloading: false
      },
      rules: {
        title: [
          { required: true, message: '请输入文章标题', trigger: 'blur' },
          { min: 5, max: 30, message: '长度在5到30之间', trigger: 'blur' }
        ],
        content: [
          { required: true, message: '请输入文章内容', trigger: 'blur' },
          { min: 20, max: 30000, message: '最少20个字', trigger: 'blur' }
        ],
        channelId: [
          { required: true, message: '请选择频道', trigger: 'blur' }
        ]
      }
    }
  },
  created () {
    this.hUploadAtical()
  },
  methods: {
    // 1.页面刷新，自动回去该文章内容
    async hUploadAtical () {
      try {
        const res = await this.$http({
          method: 'GET',
          url: '/mp/v1_0/articles/' + this.$route.params.id
        })
        console.log('获取指定的文章', res)
        // 2.获取文章的数据进行表单回填
        this.form.title = res.data.data.title
        this.form.content = res.data.data.content
        this.form.channelId = res.data.data.channel_id
        this.form.coverType = res.data.data.cover.type
        this.form.coverImages = res.data.data.cover.images
      } catch (err) {
        console.log(err)
      }
    },
    hImageTypeChange () {
      if (this.form.coverType === 1) {
        this.form.coverImages.length = 1
      } else if (this.form.coverType === 3) {
        this.form.coverImages.length = 3
      }
    },
    // 发布文章前进行表单的验证
    hEdit () {
      this.$refs.refForm.validate(valid => {
        if (valid) {
          this.doEdit()
        } else {
          this.$message.error('请将文章信息补全')
        }
      })
    },
    async doEdit () {
      // 修改isLoading的值
      this.form.isloading = true
      try {
        // 1.解构赋值
        const { title, content, channelId, coverType, coverImages } = this.form
        // 2.发送axios请求
        const res = await this.$http({
          method: 'PUT',
          url: '/mp/v1_0/articles/' + this.$route.params.id,
          data: {
            title,
            content,
            cover: {
              type: coverType,
              images: coverImages
            },
            channel_id: channelId
          }
        })
        console.log(res)
        // 3.编辑成功，消息提示
        this.$message.success('编辑文章成功')
        // 4.修改isloading的值
        this.form.isloading = false
        this.$router.push('/atical')
      } catch (err) {
        this.$message.error('编辑文章失败')
        this.form.isloading = false
      }
    }
  }
}
</script>
<style leng="less" scoped>
  .text {
    font-size: 14px;
  }
  .item {
    margin-bottom: 18px;
  }

  .clearfix:before,
  .clearfix:after {
    display: table;
    content: "";
  }
  .clearfix:after {
    clear: both
  }

  .box-card {
    width: 100%;
  }
  .publish-container /deep/ .ql-editor{
  height: 300px;
  }
</style>
